package com.xxx.util;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * rabbit mq连接
 */
public class ConnectionUtil {
	
	private static final String HOST = "127.0.0.1";
	private static final int PORT = 5672;
	private static final String VHOST = "testhost";
	private static final String USER = "guest";
	private static final String PASSWORD = "guest";

	public static Connection getConnection() throws Exception {
		// 定义连接工厂
		ConnectionFactory factory = new ConnectionFactory();
		// 设置服务地址
		factory.setHost(HOST);
		// 端口
		factory.setPort(PORT);
		// 设置账号信息，用户名、密码、vhost
		factory.setVirtualHost(VHOST);
		factory.setUsername(USER);
		factory.setPassword(PASSWORD);
		// 通过工厂获取连接
		return factory.newConnection();
	}

}
